<?php
namespace App\Services\Setting\Setting;

use App\Models\Setting\SettingCategory;
use App\Services\Manage\ManageIndex;


/**
 * @author:      李颖
 * @DateTime:    2017-06-21 14:08:39
 * @Description: 全局设置 配置分类
 */
class SettingCategoryLogin
{
    protected $settingCategoryLogic = '\App\Services\Setting\Setting\SettingCategoryLogin';
    protected $settingCategoryModel = '\App\Models\Setting\SettingCategory';
    
    use \App\Services\Goods\Traits\GetCateTree;
    use \App\Services\Goods\Traits\UpdateCate;
    use \App\Services\User\Traits\addAppId;
    /*内部获取某个分类的详情*/
    public function dbCateInfo($data)
    {
    	return SettingCategory::where([['id','=',$data['id']],['status','<>','-1']])->first();
    }

//    /*根据id获取其父类的ids数组*/
//    public function getParentIds($id): array
//    {
//        $list = $this->db()->where(['status'=>['neq',-1]])->select();
//        $ids     = $id;
//        $params  = 'id';
//        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
//        $ids = explode(",", $ids);
//        foreach ($ids as $k => &$v) {
//            $v = (int)$v;
//        }
//        array_splice($ids, array_search(0, $ids), 1);
//        return array_reverse($ids);
//    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map= [])
    {

        $listt=SettingCategory::where('status','<>',-1);
        if (isset($map['app_id'])) {
            $isManageAppId = ManageIndex::Api()->_isManageAppId($map['app_id']);
            !$isManageAppId && $listt->where('app_id','=',$map['app_id']);
        }
        if(isset($map['id'])){
            $listt->whereIn('id',$map['id']);
        }
        if(isset($map['pid'])){
            $listt->whereIn('pid',$map['pid']);
        }

        $list=$listt->get()->toArray();
        return $list;
    }

    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where[] = ['id', '=', $map['id']];
        isset($map['pid']) && $_where[] = ['pid', '=', $map['pid']];
        $_where[] = ['status', '<>', -1];
        $que = SettingCategory::where($_where);
        if (isset($map['app_id'])) {   //若卖家查看商品详情，则不传入app_id
            $manageAppId = ManageIndex::Api()->_isManageAppId($map['app_id']);
            $app_id = $map['app_id'];

            if ($manageAppId != $map['app_id']) {
                $que->Where(function ($query) use ($app_id) {
                    $query->where('app_id', $app_id)->orWhere(function ($query) use ($app_id) {
                        $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                    })->orWhere('is_public', 1);
                });
            }
        }
        $res = $que->get()->toArray();
        return $res;
    }

    /*添加一个分类*/
    public function addCategory($data): array 
    {
    	$res = SettingCategory::insert($data);
    	return auto_return($res, '添加分类成功', '添加分类失败');
    }
    
    /*编辑一个分类*/
    public function editCategory($data): array
    {
        return $this->_updateCate($this->settingCategoryLogic, $this->settingCategoryModel, $data, 'dbCateList', 'dbCateInfo');
    }

    /*获取本应用下的分类树 包含公共分类*/
    public function getTree($data): array
    {
        return $this->_getCateTree($data, $this->settingCategoryLogic, 'dbAllCateList');
    }

    /*获取某分类详情*/
    public function oneCateInfo($data): array
    {
        $_where[] = ['id', '=', $data['id']];
        $_where[] = ['status', '<>', -1];
        $que = SettingCategory::where($_where);
        if(isset($data['app_id'])) {
            $manageAppId = ManageIndex::Api()->getAppId();
            $app_id = $data['app_id'];
            if ($manageAppId != $data['app_id']) {
                $que->Where(function ($query) use ($app_id) {
                    $query->where('app_id', $app_id)->orWhere(function ($query) use ($app_id) {
                        $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                    })->orWhere('is_public', 1);
                });
            }
        }
        $res = $que->first()->toArray();
        return auto_return($res);
    }

    /*批量删除分类*/
    public function batchDelCate($data): array
    {
        $manageAppId = ManageIndex::Api()->getAppId();
        $ress = SettingCategory::where('status','<>', -1)->whereIn('id',$data['id']);
        $manageAppId != $data['app_id'] && $ress -> where('app_id','=',$data['app_id']);

        $res = $ress->update(['status'=>'-1']);
        return auto_return($res);
    }

    //批量启用/禁用选择的分类
    public function batchChangeStatus($data): array
    {
        $list = $this->dbCateList();
        is_numeric($data['id']) && $data['id'] = [$data['id']];
        foreach ($data['id'] as $k => $v) {
            $childId = child_ids($list, $v, 'id', 'pid', 'id');  //使用static数组 获取该分类下的所有子类
        }
        $data['id'] = array_merge($childId, $data['id']);
        $query=settingCategory::whereIn('id', $data['id']);
        $manageAppId = \App\Services\Manage\Api::getAppId();
        $manageAppId != $data['app_id'] && $query->where('app_id','=',$data['app_id']);
        $res=$query->update(['status'=>$data['status']]);
        return auto_return($res);
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array
    {
        $info = SettingCategory::where([['action','=',$data['action']]])->first();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

}